" want to know how long the tests take
let s:starttime = localtime()
let s:skipped = 0

" don't cause a change every time the plugin changes versions
let g:loaded_2html_plugin = '2html_test'

" remove all previous test files to make it easier to detect new files or files
" that go away
for afile in split(glob('conceallevel_test/*', 1))
  call delete(afile)
endfor
for afile in split(glob('options_test/*', 1))
  call delete(afile)
endfor
for afile in split(glob('nocopy_test/*', 1))
  call delete(afile)
endfor
for afile in split(glob('encoding_test/*', 1))
  call delete(afile)
endfor

" get the list of all *boolean* options
let optionnames = sort(keys(filter(tohtml#GetUserSettings(), 'type(v:val)==type(0)')))

" html_no_progress has no effect on generated files, remove it and turn it on
" for a speed boost
call remove(optionnames, index(optionnames, 'no_progress'))
let g:html_no_progress=1

" html_pre_wrap has minimal impact. Only 2 lines change in the entire file, in
" the CSS. Turn off for backwards compatibility and test later.
call remove(optionnames, index(optionnames, 'pre_wrap'))
let g:html_pre_wrap=0

" For testing purposes, all pages should validate for easy detection of
" screw-ups. Screw MS Word, I'll paste somewhere else to test that the copy
" prevention works.
call remove(optionnames, index(optionnames, 'no_invalid'))
let g:html_no_invalid=1

" start with no fancy unselectable stuff
let g:html_unselectable = ""

" set up for the conceallevel test by sourcing the test file with different
" types of concealable regions
edit input/test.vim
only
source %

" start with all options turned off and test each conceal level with
" and without css, with and without xhtml
for setting in optionnames
  let g:html_{setting} = 0
endfor

let cole = 0
while cole < 4
  let &conceallevel = cole
  set foldlevel=0

  let g:html_use_css = 0
  let g:html_use_xhtml = 0
  TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
  exec 'saveas! conceallevel_test/cole_'.cole.'-css.'.expand("%:t:e")
  bdelete

  let g:html_use_css = 1
  let g:html_use_xhtml = 0
  TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
  exec 'saveas! conceallevel_test/cole_'.cole.'+css.'.expand("%:t:e")
  bdelete

  let g:html_use_css = 0
  let g:html_use_xhtml = 1
  TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
  exec 'saveas! conceallevel_test/cole_'.cole.'-css.'.expand("%:t:e")
  bdelete

  let g:html_use_css = 1
  let g:html_use_xhtml = 1
  TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
  exec 'saveas! conceallevel_test/cole_'.cole.'+css.'.expand("%:t:e")
  bdelete

  let cole += 1
endwhile

" set the conceallevel back to where it is most interesting (and where it was
" set when the test output was first generated)
set conceallevel=2

" set up for a test of all the different options by opening up a diff with a
" slightly modified version of the test script (always in the same place
" regardless of user settings)
belowright vert diffsplit input/test_modified.vim

let s:done_options = []

" recursively set then clear each option and call TOhtml when all options
" are set/cleared
function! TOhtml_all_option_test(setting_list, output_dir)
  if empty(a:setting_list)
    let settings = filter(tohtml#GetUserSettings(),'type(v:val)==type(0)')
    let settings_string = join(map(sort(keys(settings)), '(settings[v:val] ? "1" : "0")'), "")
    if index(s:done_options, settings_string) == -1
      " have not yet processed this set of options, so process them
      call add(s:done_options, settings_string)
      windo set foldlevel=0
      wincmd t
      TOhtml
      1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
      exec 'saveas! '.a:output_dir.'/'.settings_string.'.'.expand("%:t:e")
      bdelete
    else
      let s:skipped+=1
    endif
  else
    let nextlist = copy(a:setting_list)
    let this_setting = remove(nextlist, 0)
    let g:html_{this_setting} = 0
    call TOhtml_all_option_test(nextlist, a:output_dir)
    let g:html_{this_setting} = 1
    call TOhtml_all_option_test(nextlist, a:output_dir)
  endif
endfunction

call TOhtml_all_option_test(optionnames, 'options_test')

let g:html_charset_override = {'utf-16': 'UTF-16', 'utf-16le' : 'UTF-16LE', 'ucs-4le' : 'UTF-32LE'}

let g:html_diff_one_file = 0

setlocal fenc=utf-16
TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
saveas! encoding_test/utf-8_enc.xhtml
bdelete

windo setlocal fenc=utf-16
TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
saveas! encoding_test/utf-16_fenc-override.xhtml
bdelete

windo setlocal fenc=utf-16le
TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
saveas! encoding_test/utf-16le_fenc-override.xhtml
bdelete

let g:html_use_encoding='UTF-32'
TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
saveas! encoding_test/ucs-4_setting.xhtml
bdelete
unlet g:html_use_encoding

windo setlocal fenc=utf-8

let g:html_use_xhtml=0

setlocal fenc=utf-16
TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
saveas! encoding_test/utf-8_enc.html
bdelete

windo setlocal fenc=utf-16
TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
saveas! encoding_test/utf-16_fenc-override.html
bdelete

windo setlocal fenc=utf-16le
TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
saveas! encoding_test/utf-16le_fenc-override.html
bdelete

let g:html_use_encoding='UTF-32LE'
TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
saveas! encoding_test/ucs-4le_setting.html
bdelete

windo setlocal fenc=utf-16

unlet g:html_charset_override
let g:html_encoding_override = {'windows-1252': '8bit-cp1252'}

let g:html_use_encoding='windows-1252'
TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
saveas! encoding_test/cp1252_override.html
bdelete

unlet g:html_use_encoding
TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
saveas! encoding_test/utf-8_fenc.html
bdelete

windo setlocal fenc=utf-8

let g:html_pre_wrap=1
let g:html_no_pre=0

TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
saveas! wrap-test.html
bdelete

let g:html_diff_one_file = 1

TOhtml
  1,10s#\m<title>\zs.*\ze</title>#\=expand("%:t:r")#
saveas! wrap-test+one-file.html
bdelete

" test uncopyable regions with a subset of options
let g:html_dynamic_folds = 1
let g:html_expand_tabs = 1
let g:html_ignore_conceal = 1
let g:html_ignore_folding = 0
let g:html_line_ids = 1
let g:html_no_invalid = 1
let g:html_no_progress = 1
let g:html_number_lines = 1
let g:html_prevent_copy = "fnd"
let g:html_use_css = 1

let option_names = [
      \ 'diff_one_file',
      \ 'hover_unfold',
      \ 'no_foldcolumn',
      \ 'no_pre',
      \ 'pre_wrap',
      \ 'use_xhtml',
      \ 'whole_filler'
      \ ]
let g:html_diff_one_file = 0
let g:html_hover_unfold = 0
let g:html_no_foldcolumn = 0
let g:html_no_pre = 0
let g:html_pre_wrap = 0
let g:html_use_xhtml = 0
let g:html_whole_filler = 0

call TOhtml_all_option_test(option_names, 'nocopy_test')

echomsg 'DONE! Test took' localtime() - s:starttime 'seconds with' s:skipped 'files skipped'

" TODO: test pre_wrap along with the other options? It caused weird issues
" with the diff mode at first
" TODO: test line ranges: specifically test for the issue fixed in v10
" TODO: test font
" TODO: test more encodings?
" TODO: test special buffer encoding detection?

" TODO: test html_no_progress and any future UI-only options to make sure they
" don't have any affect on output?

" TODO: automatically update a file (results.txt) with the review status of each
" file (e.g. validates, looks good, or not reviewed)
